const activityModel = require("../../models/wechat/activity");

module.exports = {
  // 添加活动
  async add(req, res) {
    console.log("接收到添加活动请求参数：", req.body);
    if (
      !req.body.title ||
      !req.body.body ||
      !req.body.dateSel ||
      !req.body.address ||
      !req.body.address.name ||
      !req.body.address.latitude ||
      !req.body.address.longitude ||
      !req.body.img ||
      !req.body.openid
    ) {
      return res.json({
        code: "1001",
        msg: "参数不正确",
      });
    }
    const data = [
      req.body.title,
      req.body.body,
      req.body.dateSel,
      Number(req.body.count),
      req.body.address.address,
      req.body.address.name,
      req.body.address.latitude,
      req.body.address.longitude,
      req.body.img,
      req.body.openid, // 活动创建者openid
    ];
    console.log("-----", data);
    const resp = await activityModel.add(data);
    console.log("插入数据库：", resp);

    res.json({
      code: "200",
      msg: "ok",
    });
  },
  // 获取活动列表
  async list(req, res) {
    let { currentPage, pageSize } = req.query;
    currentPage = parseInt(currentPage);
    pageSize = parseInt(pageSize);
    console.log("活动列表参数", currentPage, pageSize);
    const startIndex = (currentPage - 1) * pageSize;

    if (!currentPage || !pageSize) {
      return res.json({
        code: "1001",
        msg: "参数错误",
      });
    }

    const resp = await activityModel.list([startIndex, pageSize]);
    // console.log('查询活动列表：', resp);

    res.json({
      code: "200",
      msg: "ok",
      data: resp,
    });
  },
  // 活动详情
  async detail(req, res) {
    let { id } = req.query;
    id = Number(id);
    if (!id) {
      return res.json({
        code: "1001",
        msg: "参数不正确",
      });
    }

    const resp = await activityModel.detail([id]);
    console.log("详情数据:", resp);

    if (resp.length === 0) {
      return res.json({
        code: "1001",
        msg: "数据不存在",
      });
    }

    res.json({
      code: "200",
      msg: "ok",
      data: resp[0],
    });
  },
  // 参与活动
  async join(req, res) {
    const { openid, activity_id } = req.query;
    if (!openid || !activity_id) {
      return res.json({
        code: "1001",
        msg: "参数不正确",
      });
    }

    const respFindUser = await activityModel.findUser(openid);
    console.log("确认用户存在", respFindUser);
    if (respFindUser.length == 0) {
      return res.json({
        code: "1001",
        msg: "非法操作",
      });
    }

    const respDetail = await activityModel.detail([activity_id]);
    console.log("详情数据:", respDetail);

    const respAddCount = await activityModel.addCount([
      respDetail[0].current_count + 1,
      activity_id,
    ]);
    console.log("操作数量增加", respAddCount);

    const respAddUser = await activityModel.addUser([activity_id, openid]);
    console.log("将用户存在活动详情表", respAddUser);

    // 新建一条参与活动的通知
    // (NULL, '您的活动有新参与者',2,'activity_id',5,'参与者openid',1,'活动发布者id',DEFAULT,DEFAULT)
    const joinNotifyRes = await activityModel.activityJoinNotify([activity_id, openid, respDetail[0].openid]);
    console.log("新建活动通知：", joinNotifyRes);

    res.json({
      code: "200",
      msg: "OK",
    });
  },
  // 活动封面图上传
  uploadpreview(req, res) {
    console.log("文件上传：", req.file);
    console.log("文件上传其他信息：", req.body);
    const url = 'http://127.0.0.1:3000/uploads/' + req.file.filename

    res.json({
      code: '200',
      msg: 'ok',
      data: url
    });
  },
  // 订阅消息
  subscribeMessage(req, res) { },
  // 活动通知
  async activityJoinNotify(req, res) {
    try {
      const { openid } = req.query;
      console.log('查询新消息时获取到openid：', openid);

      if (!openid) {
        return res.json({
          code: '1001',
          msg: '参数错误'
        })
      }

      const notifyRes = await activityModel.activityNotify([openid]);
      console.log('消息未读结果：', notifyRes);

      res.json({
        code: '200',
        msg: 'ok',
        data: notifyRes
      })
    } catch (err) {
      console.log('查询未读消息出错：', err);
      res.json({
        code: '1001',
        msg: '查询错误'
      })
    }
  }
};
